home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Environments / Small Eiffel 0.4.8 / lib_std / collection2.e < prev    next >
Text File  |  1997-04-13  |  6KB  |  257 lines

  1. -- Part of SmallEiffel -- Read DISCLAIMER file -- Copyright (C) 
  2. -- Dominique COLNET and Suzanne COLLIN -- colnet@loria.fr
  3. --
  4. deferred class COLLECTION2[E]
  5.    -- 
  6.    -- Abstract definition of a 2 dimensional collection of elements
  7.    -- of type E. Each element can be accessed using a couple <i1,i2>
  8.    -- of INTEGER.
  9.    -- 
  10.    -- The SmallEiffel standard library (lib_std) provides three
  11.    -- implementations : ARRAY2[E], FIXED_ARRAY2[E] and LINK_ARRAY2[E].
  12.    -- All implementations have exactly the same behavior. Switching 
  13.    -- from one implementation to another only change the memory used
  14.    -- and the execution time.
  15.    --
  16.  
  17. inherit
  18.    ANY
  19.       undefine copy 
  20.       redefine is_equal, fill_tagged_out_memory
  21.       end;
  22.  
  23. feature -- Indexing :
  24.  
  25.    lower1, lower2: INTEGER is
  26.      -- Lower index bounds.
  27.       deferred
  28.       end;
  29.  
  30.    upper1, upper2: INTEGER is
  31.      -- Upper index bounds.
  32.       deferred
  33.       end;
  34.  
  35. feature -- Reading :
  36.  
  37.    item(i1, i2: INTEGER): E is
  38.       require
  39.      valid_index(i1,i2);
  40.       deferred
  41.       end;
  42.  
  43. feature -- Writing :
  44.  
  45.    put(x: like item; i1, i2: INTEGER) is
  46.       require
  47.      valid_index(i1,i2);
  48.       deferred
  49.       ensure
  50.      item(i1,i2) = x
  51.       end;
  52.    
  53. feature -- Index validity :
  54.  
  55.    valid_index1(i1: INTEGER): BOOLEAN is
  56.       do
  57.      Result := lower1 <= i1 and then i1 <= upper1;
  58.       ensure
  59.      Result implies lower1 <= i1 and i1 <= upper1
  60.       end;
  61.       
  62.    valid_index2(i2: INTEGER): BOOLEAN is
  63.       do
  64.      Result := lower2 <= i2 and then i2 <= upper2;
  65.       ensure
  66.      Result implies lower2 <= i2 and i2 <= upper2
  67.       end;
  68.       
  69.    valid_index(i1, i2: INTEGER): BOOLEAN is
  70.       do
  71.      Result := lower1 <= i1
  72.                  and then
  73.            i1 <= upper1 
  74.                      and then
  75.            lower2 <= i2 
  76.                      and then
  77.            i2 <= upper2; 
  78.       ensure
  79.      Result implies valid_index1(i1) and valid_index2(i2)
  80.       end;
  81.  
  82. feature -- Counting :
  83.  
  84.    count1: INTEGER is
  85.      -- Size of the first dimension.
  86.       deferred
  87.       ensure
  88.      Result = upper1 - lower1 + 1;
  89.       end;
  90.  
  91.    count2: INTEGER is
  92.      -- Size of the second dimension.
  93.       deferred
  94.       ensure
  95.      Result = upper2 - lower2 + 1;
  96.       end;
  97.  
  98.    count: INTEGER is
  99.       -- Total number of elements.
  100.       do
  101.      Result := count1 * count2;
  102.       end;
  103.    
  104. feature 
  105.  
  106.    swap(i1, j1, i2, j2: INTEGER) is
  107.       require
  108.      valid_index(i1,j1);
  109.      valid_index(i2,j2)
  110.       local
  111.      tmp: like item;
  112.       do
  113.      tmp := item(i1,j1);
  114.      put(item(i2,j2),i1,j1);
  115.      put(tmp,i2,j2);
  116.       end;
  117.  
  118.    set_all_with(v: like item) is
  119.      -- Set all item with value `v'.
  120.       deferred
  121.       ensure
  122.      count = old count
  123.       end;
  124.    
  125.     clear_all is
  126.      -- Set all items to default values.
  127.       local
  128.      value: like item;
  129.       do
  130.      set_all_with(value);
  131.       ensure
  132.      count = old count;
  133.       end;
  134.    
  135. feature -- Looking and comparison :
  136.  
  137.    is_equal(other: like Current): BOOLEAN is
  138.       local
  139.      i1, i2: INTEGER;
  140.       do
  141.      if Current = other then
  142.         Result := true;
  143.      elseif lower1 /= other.lower1 then
  144.      elseif upper1 /= other.upper1 then
  145.      elseif lower2 /= other.lower2 then
  146.      elseif upper2 /= other.upper2 then
  147.      else
  148.         from
  149.            Result := true;
  150.            i1 := upper1;
  151.         until
  152.            not Result or else i1 < lower1
  153.         loop
  154.            from
  155.           i2 := upper2;
  156.            until
  157.           not Result or else i2 < lower2
  158.            loop
  159.           Result := equal_like(item(i1,i2),other.item(i1,i2));
  160.           i2 := i2 - 1;
  161.            end;
  162.            i1 := i1 - 1;
  163.         end;
  164.      end;
  165.       end;
  166.    
  167.    all_cleared: BOOLEAN is
  168.      -- Are all items set to default values?
  169.       local
  170.      value: like item;
  171.      i: INTEGER;
  172.       do
  173.      from  
  174.         Result := true;
  175.         i := lower;
  176.      until
  177.         not Result or else i > upper
  178.      loop
  179.         Result := value = item(i);
  180.         i := i + 1;
  181.      end;
  182.       end;
  183.  
  184. feature -- Printing :
  185.  
  186.    fill_tagged_out_memory is
  187.       local
  188.      i1, i2: INTEGER;
  189.      v: like item;
  190.       do
  191.      tagged_out_memory.append("lower1: "); 
  192.      lower1.append_in(tagged_out_memory);
  193.      tagged_out_memory.append(" upper1: "); 
  194.      upper1.append_in(tagged_out_memory);
  195.      tagged_out_memory.append(" lower2: "); 
  196.      lower2.append_in(tagged_out_memory);
  197.      tagged_out_memory.append(" upper2: "); 
  198.      upper2.append_in(tagged_out_memory);
  199.      tagged_out_memory.append(" [");
  200.      from  
  201.         i1 := lower1;
  202.         i2 := lower2;
  203.      until
  204.         not valid_index(i1,i2) or else 
  205.         tagged_out_memory.count > 2048
  206.      loop
  207.         v := item(i1,i2);
  208.         if v = Void then
  209.            tagged_out_memory.append("Void");
  210.         else
  211.            v.out_in_tagged_out_memory;
  212.         end;
  213.         i2 := i2 + 1;
  214.         if i2 > upper2 then
  215.            i2 := lower2;
  216.            i1 := i1 + 1;
  217.         end;
  218.         if valid_index(i1,i2) then
  219.            tagged_out_memory.extend(' ');
  220.         end;
  221.      end;
  222.      if valid_index(i1,i2) then
  223.         tagged_out_memory.append(" ..."); 
  224.      end;
  225.      tagged_out_memory.extend(']'); 
  226.       end;
  227.  
  228. feature -- The Guru section :
  229.  
  230.    free is
  231.      -- Free the memory used by the Current COLLECTION (objects
  232.      -- pointed by the Current COLLECTION are not affected). 
  233.      -- Assume you don't use Current any more. 
  234.       deferred
  235.       end;
  236.    
  237. feature {NONE}
  238.  
  239.    frozen equal_like(e1, e2: like item): BOOLEAN is
  240.      -- Note: this feature is called to avoid calling `equal'
  241.      -- on expanded types (no automatic conversion to 
  242.      -- corresponding reference type).
  243.       do
  244.      if e1.is_basic_expanded_type then
  245.         Result := e1 = e2;
  246.      elseif e1.is_expanded_type then
  247.         Result := e1.is_equal(e2);
  248.      elseif e1 = e2 then
  249.         Result := true;
  250.      elseif e1 = Void or else e2 = Void then
  251.      else
  252.         Result := e1.is_equal(e2);
  253.      end;
  254.       end;
  255.  
  256. end -- COLLECTION2
  257.